app.controller("addBook", function ($http, $scope, $location) {
        $scope.reader = new FileReader();
        $scope.bookname = null;
        $scope.bookNum = null;
        $scope.bookday = null;
        $scope.bookaddress = null;
        $scope.bookInfo = null;
        $scope.bookimg = null;
        var categoryid;
        $scope.gtecategory = function ($event) {
            categoryid = $($event.target).attr("id");
            $('#dropdownMenu1').html($($event.target).html());
            console.log(categoryid);
        };
        $scope.bookCategory = null;
        $http({
            method: 'Get',
            url: app.url + 'Book/Category'
        }).then(function (res) {
            console.log(res.data);
            $scope.bookCategory = res.data;
        }, function () {
            $('.dropdownMenu1').val("未查到数据");
        });

        $scope.img64 = null;
        function getObjectURL(file) {
            var url = null;
            if (window.createObjectURL != undefined) { // basic
                url = window.createObjectURL(file);
            } else if (window.URL != undefined) { // mozilla(firefox)
                url = window.URL.createObjectURL(file);
            } else if (window.webkitURL != undefined) { // webkit or chrome
                url = window.webkitURL.createObjectURL(file);
            }
            return url;
        }

        $scope.img_upload = function (files) {
            $scope.reader.readAsDataURL(files[0]);  //FileReader的方法，把图片转成base64
            var objUrl = getObjectURL(files[0]);
            $('#pic').attr('src', objUrl);
        };

        $scope.reader.onload = function (ev) {
            $scope.$apply(function () {
                var img = ev.target.result;//接收base64

               img = img.replace("data:image/png;base64,", '');
                img = img.replace("data:image/jpg;base64,", '');
                img = img.replace("data:image/jpeg;base64,", '');


                $scope.bookimg=img;
            });
        };

        $scope.postBook = function () {
            console.log($scope.bookimg);
            var book = {
                'bookName': $scope.bookname, 'bookAddress': $scope.bookaddress,
                'categoryId': categoryid, 'bookNum': $scope.bookNum,
                'bookIntroduction': $scope.bookInfo, 'bookBorrowDay': $scope.bookday,
                'bookimg':$scope.bookimg
            };
            console.log(book);
            $http({
                method: 'POST',
                url: app.url + 'Book/add',
                data: book,

            }).then(function (res) {
                console.log(res.data);
                $location.path('/index-content');
            }, function () {
                $('.dropdownMenu1').val("未查到数据");
            })
        }
    }
);